const db =wx.cloud.database()
const pics =db.collection('pics')
const votes = db.collection('votes')
const $=db.command.aggregate
const time = new Date()
const rq = time.toLocaleDateString();
Page({
    data:{
        mavmax:-1
    },
    async onLoad(option){
        let res = await wx.cloud.callFunction({
            name:"getPics"
        })
        console.log(res);
        let plist = res.result.plist
        this.setData({
            plist
        }) 
    
            res=await votes.aggregate()
            .group({
                _id:'$fileid',
                count:$.sum(1),
            })
            .end()
            let vlist=res.list
            this.setData({
                vlist
            })
            
            let fileid=this.data.fileid;
            let fileid1=this.data.fileid1;
            //插入图
            if(fileid1){
                let index =this.data.plist.findIndex(v=>{
                  return v.fileid== fileid1
                })
                this.setData({index,fileid1:""})
                this.count(index)
            
             }
             //投票
            if(fileid){
                let index =this.data.plist.findIndex(v=>{
                  return v.fileid== fileid
                })
                plist.forEach(e=>{
                    if(e.fileid==fileid)
                    e.class="borde"
                })
                this.setData({plist,fileid:""});
   
                this.count(index)
             }
        
        res = await wx.cloud.callFunction({
            name:'login'
        })
        let openid = res.result.openid
        this.setData({
            openid
        })
        res = await votes.aggregate()
        .match({
            _openid : openid
        }).group({
            _id:"$fileid",
            count:$.sum(1)
        }).end()
      
        let p=await votes.where({
            time:rq,
            _openid:openid
        }).get();
    
        let mavmax = 3-p.data.length;
        this.setData({
            mavmax
        })

        if(res.list.length){
      
            let votecount = res.list[0].count
            this.setData({
                votecount
            })

        plist.forEach(e=>{
            for(let j in p.data){
            if(e.fileid==p.data[j].fileid){
                e.class="borde";
            }
            }
        })

        this.setData({
            plist
        })   
    }
 
  
    },
    tap(e){
        let mavmax = this.data.mavmax-1;
        this.setData({
            mavmax
        })
        if(mavmax<=-1){
            wx.showToast({
                title:"超过本日投票次数",
               icon:"none"
            })
            return
        }
        votes.add({
            data:{
                fileid:e.currentTarget.dataset.id,
                time:rq
            }
        }).then(res=>{
            if(res.errMsg.indexOf('ok')>-1){
                wx.showToast({
                    title:'还有'+(mavmax)+"次投票机会",
                   icon:"none"
                })
            }
        })
        this.setData({
            fileid:e.currentTarget.dataset.id
        })
        this.onLoad()
    },
    async long(){
        let res =await wx.chooseImage({
          count: 1,
        })
        console.log(res)
        let filename =res.tempFilePaths[0]
        let purefilename =filename.split('/').slice(-1)[0]
        res=await wx.cloud.uploadFile({
            cloudPath:'ppp/'+purefilename,
            filePath:filename
        })
        let fileid=res.fileID
        res =await pics.add({
            data:{
                fileid
            }
        })
        if(res.errMsg.indexOf('ok')>-1){
            wx.showToast({
                title:'上传图片成功',
            })
        }
        this.setData({
            fileid1:fileid
        })
        this.onLoad()
    },
    count(index){
        let v=this.data.vlist.find(v=>{
            return v._id==this.data.plist[index].fileid
        })
        let count =0
        if(v){
            count=v.count
        }
        wx.setNavigationBarTitle({
          title: index+1+'/'+this.data.plist.length+' '+count+'票',
        })
    },
    change(e){
        this.count(e.detail.current)
    },
    async clear(e){
      let re= await votes.where({
         _openid:"oMqfa5B82P-UZ5QFxruCxqJQNKt4"
      }).remove(); 
      console.log(re);
    }
})